// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-12-3

#include "stdafx.h"
#include "ui_attribview.h"
#include "ui_svrwnd.h"


static const UINT JOY_SERVER_ATTRIB_TIMER = 0x3301;

namespace JoyAttribViewFields
{

class AttribNameField
    : public CJoyAttribViewField
{
};

class AttribValueField
    : public CJoyAttribViewField
{
};

} // namespace JoyAttribViewFields

// class CJoyAttribViewItem

CJoyAttribViewItem::CJoyAttribViewItem()
{
}

CJoyAttribViewItem::~CJoyAttribViewItem()
{
}

void CJoyAttribViewItem::UpdateAttrib(CJoyServer* pServer)
{
    pServer;
    UpdateAttrib();
}

void CJoyAttribViewItem::UpdateAttrib()
{
}

void CJoyAttribViewItem::OnStart(CJoyServer* pServer)
{
    UpdateAttrib(pServer);
}

void CJoyAttribViewItem::OnStop()
{
    m_attribValue.Empty();
}

// #define JOY_ATTRIB_SEL_DRAW

void CJoyAttribViewItem::CM_Draw(Graphics& graph)
{
    CJoyAttribView* attribView = (CJoyAttribView*)m_listView;

    int w = m_area.Width();
    int h = m_area.Height();

    Color lightFrame, darkFrame, backColor, textColor;
#ifdef JOY_ATTRIB_SEL_DRAW
    if(IsSel())
    {
        lightFrame.SetValue(0xFF999999);
        darkFrame.SetValue(0xFF404040);
        backColor.SetValue(0xFF777777);
        textColor.SetValue(0xFFFFFFFF);
    }
    else
#endif
    {
        lightFrame.SetValue(0xFFFFFFFF);
        darkFrame.SetValue(0xFFAFAFAF);
        backColor.SetValue(0xFFF7F7F7);
        textColor.SetValue(0xFF000000);
    }
    Pen lightPen(lightFrame), darkPen(darkFrame);
    SolidBrush backBrush(backColor), txBrush(textColor);

    // draw back.
    {
        Point p0(0, 0), p1(w-1, 0);
        Point p3(0, h-1), p2(w-1, h-1);

        // bottom line
        graph.DrawLine(&darkPen, p3, p2);

        // back rect
        graph.FillRectangle(&backBrush, Rect(1, 1, w-1, h-2));
    
        // top line
#ifdef JOY_ATTRIB_SEL_DRAW
        if(IsSel())
        {
            graph.DrawLine(&darkPen, p0, p1);
            p0.Y += 1; p1.Y += 1;
            graph.DrawLine(&lightPen, p0, p1);
        }
        else
#endif
        {
            graph.DrawLine(&lightPen, p0, p1);
        }

        // left line
        graph.DrawLine(&lightPen, p0, p3);
        // division line.
        POSITION pos = attribView->m_fields.GetHeadPosition();
        while(pos != NULL)
        {
            CJoyListViewField* pField = attribView->m_fields.GetNext(pos);
            if(!pField->IsVisible())
            {
                continue;
            }
            int x1 = pField->GetArea().left;
            int x2 = pField->GetArea().right-1;
            graph.DrawLine(&lightPen, x1, 0, x1, h);
            graph.DrawLine(&darkPen, x2, 0, x2, h);
        }

        // graph.DrawLine(&darkPen, p1, p2);
    }

    StringFormat sf;
    sf.SetLineAlignment(StringAlignmentCenter);
    sf.SetTrimming(StringTrimmingEllipsisCharacter);
    sf.SetFormatFlags(StringFormatFlagsNoWrap);
    FontFamily fam(attribView->m_textFace);
    Font font(&fam, (REAL)attribView->m_textSize, FontStyleRegular, UnitPixel);

    // draw attribute name.
    {
        int textLeft = 4;
        int fw = attribView->m_attribNameField->GetArea().Width();
        int fx = attribView->m_attribNameField->GetArea().left;

        graph.DrawString(
        m_attribName, m_attribName.GetLength(),
        &font,
        RectF((REAL)fx+textLeft, (REAL)0, (REAL)fw-textLeft, (REAL)h),
        &sf, &txBrush);
    }

    // draw attribute value.
    {
        int textLeft = 4;
        int fw = attribView->m_attribValueField->GetArea().Width();
        int fx = attribView->m_attribValueField->GetArea().left;

        graph.DrawString(
        m_attribValue, m_attribValue.GetLength(),
        &font,
        RectF((REAL)fx+textLeft, (REAL)0, (REAL)fw-textLeft, (REAL)h),
        &sf, &txBrush);
    }

}

// class CJoyAttribViewField

void CJoyAttribViewField::CM_Draw(Graphics& graph)
{
    graph;
}

// class CJoyAttribView

IMPLEMENT_DYNAMIC(CJoyAttribView, CJoyListView)

CJoyAttribView::CJoyAttribView()
{
    m_svrWnd    = NULL;
}

CJoyAttribView::~CJoyAttribView()
{
}

void CJoyAttribView::Start()
{
    CJoyServer* pServer = m_svrWnd->m_server;
    ASSERT(pServer != NULL);

    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CJoyAttribViewItem* item = (CJoyAttribViewItem*)m_items.GetNext(pos);
        item->OnStart(pServer);
        item->InvalidateCtrl();
    }
}

void CJoyAttribView::Stop()
{
    POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CJoyAttribViewItem* item = (CJoyAttribViewItem*)m_items.GetNext(pos);
        item->OnStop();
        item->InvalidateCtrl();
    }
}

void CJoyAttribView::UpdateAllAttrib()
{
    CJoyServer* pServer = m_svrWnd->m_server;
    if(pServer != NULL && pServer->m_started)
    {
        POSITION pos = m_items.GetHeadPosition();
        while(pos)
        {
            CJoyAttribViewItem* item = (CJoyAttribViewItem*)m_items.GetNext(pos);
            item->UpdateAttrib(pServer);
            //item->InvalidateCtrl();
        }
    }
    else
    {
        POSITION pos = m_items.GetHeadPosition();
        while(pos)
        {
            CJoyAttribViewItem* item = (CJoyAttribViewItem*)m_items.GetNext(pos);
            item->UpdateAttrib();
            //item->InvalidateCtrl();
        }
    }

    this->m_itemPanel->InvalidateCtrl();
}
//*-----------------------------------------------------updateNewRoom
void CJoyAttribView::UpdateAllNewRoom(){
	UINT roomIds[10]={0}, roomCount=0;
	
	if(m_svrWnd->m_server==NULL){
		return ;
	}
	CJoyServer* pServer = m_svrWnd->m_server;

	pServer->UpdateUserSets( roomIds, &roomCount);

	for( UINT uLoop=0; uLoop<roomCount; uLoop++ ){
		m_svrWnd->UpdateInsertAttrib(roomIds[uLoop]);
	}
}
//------------------------------------------------------------*/
BEGIN_MESSAGE_MAP(CJoyAttribView, CJoyListView)
    ON_WM_CREATE()
    ON_WM_DESTROY()
    ON_WM_TIMER()
END_MESSAGE_MAP()

int CJoyAttribView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    ASSERT(m_svrWnd != NULL);

    if (CJoyListView::OnCreate(lpCreateStruct) == -1)
        return -1;

    AddField_T(m_attribNameField, "attrib_name");
    AddField_T(m_attribValueField, "attrib_value");

    // create timer to update server attributes.
    SetTimer(JOY_SERVER_ATTRIB_TIMER, 10000, NULL);

    return 0;
}

void CJoyAttribView::OnDestroy()
{
    KillTimer(JOY_SERVER_ATTRIB_TIMER);

    CJoyListView::OnDestroy();
}

void CJoyAttribView::OnTimer(UINT nIDEvent)
{
    if(nIDEvent != JOY_SERVER_ATTRIB_TIMER)
    {
        CJoyListView::OnTimer(nIDEvent);
        return;
    }
	//UpdateAllNewRoom();//-----------------------------------------------------updateNewRoom

    UpdateAllAttrib();
	
	
}
